package com.example.cet4try.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cet4try.R;
import com.example.cet4try.Utils.MyAsyncTask;
import com.example.cet4try.Utils.MySQLUtils;

import java.util.HashMap;
import java.util.Map;

public class ActivityFeedback extends AppCompatActivity {

    private Handler handler;
    private Map<String,String> feedback = new  HashMap<String,String>();
    private EditText ed_feedback;
    private TextView word_count;
    private boolean sendFlag;
    private String username,nickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        ed_feedback = findViewById(R.id.ed_feedback);
        ed_feedback.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = editable.toString();
                int len = 0;
                if (!TextUtils.isEmpty(s))
                    len = s.length();
                Message msg = Message.obtain();
                msg.what = 0;
                msg.obj = len;
                handler.sendMessage(msg);
            }
        });

        word_count = findViewById(R.id.word_count);

        SharedPreferences preferences = getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");
        nickname = preferences.getString("nickname","");

        Button button = findViewById(R.id.btn_feedback);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String string = ed_feedback.getText().toString();
                if (TextUtils.isEmpty(string)){
                    Toast.makeText(ActivityFeedback.this,"请先输入内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (string.length() < 10){
                    Toast.makeText(ActivityFeedback.this,"字数不到10！",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (string.length() > 300){
                    Toast.makeText(ActivityFeedback.this,"字数超出限制！",Toast.LENGTH_SHORT).show();
                    return;
                }
                initFeedback();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // 调用数据库工具类DBUtils的getInfoByName方法获取数据库表中数据
                        Message message = handler.obtainMessage();
                        message.what = 1;
                        message.obj = MySQLUtils.getInternetTime();
                        //String time = Tools.getSystemTime();

                        // 发消息通知主线程更新UI
                        handler.sendMessage(message);
                    }
                }).start();
            }


        });

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case 0:{
                        int len = (int) msg.obj;
                        word_count.setText(len+"/300");
                    }break;
                    case 1:{
                        String time = (String) msg.obj;
                        if (time != null) {
                            feedback.put("time", time);
                            sendFeedback();
                        }
                        else
                            Toast.makeText(ActivityFeedback.this, "反馈失败，请检查网络连接", Toast.LENGTH_SHORT).show();
                    }break;

                }

            }
        };

    }
    private void initFeedback() {
        //time,content,user,nick
        feedback.put("content",ed_feedback.getText().toString());
        feedback.put("user",username);
        feedback.put("nick",nickname);

    }

    private void sendFeedback(){
        MyAsyncTask task = new MyAsyncTask(ActivityFeedback.this,"正在提交");
        task.setOnExecute(new MyAsyncTask.OnExecute() {
            @Override
            public void before() {
                sendFlag = false;
            }

            @Override
            public void main() {
                sendFlag = MySQLUtils.submitFeedback(feedback);
            }

            @Override
            public void after() {
                if (sendFlag){
                    Toast.makeText(ActivityFeedback.this,"反馈成功",Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(ActivityFeedback.this,"反馈失败，请检查网络连接",Toast.LENGTH_SHORT).show();
            }
        });
        task.execute();
    }


    @Override
    protected void onDestroy() {
        handler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }


}
